home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / modes / view.el.z / view.el
Encoding:
Text File  |  1998-05-21  |  17.9 KB  |  480 lines

  1. ;;; view.el --- peruse file or buffer without editing.
  2.  
  3. ;; Copyright (C) 1985, 1989, 1994, 1995 Free Software Foundation, Inc.
  4.  
  5. ;; Author: K. Shane Hartman
  6. ;; Keywords: wp unix
  7. ;; Maintainer: FSF
  8.  
  9. ;; This file is part of XEmacs.
  10.  
  11. ;; XEmacs is free software; you can redistribute it and/or modify it
  12. ;; under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; XEmacs is distributed in the hope that it will be useful, but
  17. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19. ;; General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  23. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  24. ;; 02111-1307, USA.
  25.  
  26. ;;; Synched up with: FSF 19.34.
  27.  
  28. ;;; Commentary:
  29.  
  30. ;; This package provides the `view' minor mode documented in the Emacs
  31. ;; user's manual.
  32.  
  33. ;; XEmacs: We don't autoload this because we use `view-less' instead.
  34. ;; #### I junked the old version and replaced it with FSF's latest version.
  35. ;; Perhaps something needs integrating into view-less.
  36.  
  37. ;;; Code:
  38.  
  39. ;; XEmacs (to try to forestall complaints).
  40. (when (featurep 'view-less)
  41.   (error "Can't load view.el and view-less.el in same session."))
  42.  
  43. (defvar view-highlight-face 'highlight
  44.   ;; XEmacs change
  45.    "*The extent face used for highlighting the match found by View mode search.")
  46.  
  47. (defvar view-mode nil "Non-nil if View mode is enabled.")
  48. (make-variable-buffer-local 'view-mode)
  49.  
  50. (defvar view-mode-auto-exit nil
  51.   "Non-nil means scrolling past the end of buffer exits View mode.
  52. Some commands, such as \\[view-file], set this to t locally;
  53. +the only way to override that is to set it to nil using `view-mode-hook'.")
  54.  
  55. (make-variable-buffer-local 'view-mode-auto-exit)
  56.  
  57. (defvar view-old-buffer-read-only nil)
  58. (make-variable-buffer-local 'view-old-buffer-read-only)
  59. (defvar view-old-Helper-return-blurb)
  60. (make-variable-buffer-local 'view-old-Helper-return-blurb)
  61.  
  62. (defvar view-scroll-size nil)
  63. (make-variable-buffer-local 'view-scroll-size)
  64.  
  65. (defvar view-last-regexp nil)
  66. (make-variable-buffer-local 'view-last-regexp)
  67.  
  68. (defvar view-exit-action nil)
  69. (make-variable-buffer-local 'view-exit-action)
  70. (defvar view-return-here nil)
  71. (make-variable-buffer-local 'view-return-here)
  72. (defvar view-exit-position nil)
  73. (make-variable-buffer-local 'view-exit-position)
  74.  
  75. ;; XEmacs change
  76. (defvar view-extent nil
  77.   "Extent used to display where a search operation found its match.
  78. This is local in each buffer, once it is used.")
  79. (make-variable-buffer-local 'view-extent)
  80.  
  81. (or (assq 'view-mode minor-mode-alist)
  82.     (setq minor-mode-alist
  83.       (cons '(view-mode " View") minor-mode-alist)))
  84.  
  85. (defvar view-mode-map nil)
  86. (if view-mode-map
  87.     nil
  88.   (setq view-mode-map (make-keymap 'view-mode-map))
  89.   ;; We used to call suppress-keymap here, but that isn't good in a minor mode.
  90.   ;; Self-inserting characters will beep anyway, since the buffer is read-only,
  91.   ;; and we should not interfere with letters that serve as useful commands.
  92.   (define-key view-mode-map "q" 'view-exit)
  93.   (define-key view-mode-map "<" 'beginning-of-buffer)
  94.   (define-key view-mode-map ">" 'end-of-buffer)
  95.   (define-key view-mode-map "\ev" 'View-scroll-lines-backward)
  96.   (define-key view-mode-map "\C-v" 'View-scroll-lines-forward)
  97.   (define-key view-mode-map " " 'View-scroll-lines-forward)
  98.   (define-key view-mode-map "\C-?" 'View-scroll-lines-backward)
  99.   (define-key view-mode-map "\n" 'View-scroll-one-more-line)
  100.   (define-key view-mode-map "\r" 'View-scroll-one-more-line)
  101.   (define-key view-mode-map "z" 'View-scroll-lines-forward-set-scroll-size)
  102.   (define-key view-mode-map "g" 'View-goto-line)
  103.   (define-key view-mode-map "=" 'what-line)
  104.   (define-key view-mode-map "." 'set-mark-command)
  105.   (define-key view-mode-map "'" 'View-back-to-mark)
  106.   (define-key view-mode-map "@" 'View-back-to-mark)  
  107.   (define-key view-mode-map "x" 'exchange-point-and-mark)
  108.   (define-key view-mode-map "h" 'describe-mode)
  109.   (define-key view-mode-map "?" 'describe-mode)
  110.   (define-key view-mode-map "s" 'isearch-forward)
  111.   (define-key view-mode-map "r" 'isearch-backward)
  112.   (define-key view-mode-map "/" 'View-search-regexp-forward)
  113.   (define-key view-mode-map "\\" 'View-search-regexp-backward)
  114.   ;; This conflicts with the standard binding of isearch-regexp-forward
  115.   (define-key view-mode-map "\e\C-s" 'View-search-regexp-forward)
  116.   (define-key view-mode-map "\e\C-r" 'View-search-regexp-backward)  
  117.   (define-key view-mode-map "n" 'View-search-last-regexp-forward)
  118.   (define-key view-mode-map "p" 'View-search-last-regexp-backward)
  119.   )
  120.  
  121. (or (assq 'view-mode minor-mode-map-alist)
  122.     (setq minor-mode-map-alist
  123.       (cons (cons 'view-mode view-mode-map) minor-mode-map-alist)))
  124.  
  125.  
  126. (defun view-file (file-name)
  127.   "View FILE in View mode, returning to previous buffer when done.
  128. The usual Emacs commands are not available; instead,
  129. a special set of commands (mostly letters and punctuation)
  130. are defined for moving around in the buffer.
  131. Space scrolls forward, Delete scrolls backward.
  132. For list of all View commands, type ? or h while viewing.
  133.  
  134. This command runs the normal hook `view-mode-hook'."
  135.   (interactive "fView file: ")
  136.   (let ((old-buf (current-buffer))
  137.     (had-a-buf (get-file-buffer file-name))
  138.     (buf-to-view (find-file-noselect file-name)))
  139.     ;; This used to pass t as second argument,
  140.     ;; but then the buffer did not show up in the Buffers menu.
  141.     (switch-to-buffer buf-to-view had-a-buf)
  142.     (view-mode-enter old-buf
  143.              (and (not had-a-buf) (not (buffer-modified-p buf-to-view))
  144.               'kill-buffer))))
  145.  
  146. (defun view-file-other-window (file-name)
  147.   "View FILE in View mode in other window.
  148. Return to previous buffer when done.
  149. The usual Emacs commands are not available; instead,
  150. a special set of commands (mostly letters and punctuation)
  151. are defined for moving around in the buffer.
  152. Space scrolls forward, Delete scrolls backward.
  153. For list of all View commands, type ? or h while viewing.
  154.  
  155. This command runs the normal hook `view-mode-hook'."
  156.   (interactive "fView file: ")
  157.   (let ((old-arrangement (current-window-configuration))
  158.     (had-a-buf (get-file-buffer file-name))
  159.     (buf-to-view (find-file-noselect file-name)))
  160.     (switch-to-buffer-other-window buf-to-view)
  161.     (view-mode-enter old-arrangement
  162.              (and (not had-a-buf) (not (buffer-modified-p buf-to-view))
  163.               'kill-buffer))))
  164.  
  165. (defun view-buffer (buffer-name)
  166.   "View BUFFER in View mode, returning to previous buffer when done.
  167. The usual Emacs commands are not available; instead,
  168. a special set of commands (mostly letters and punctuation)
  169. are defined for moving around in the buffer.
  170. Space scrolls forward, Delete scrolls backward.
  171. For list of all View commands, type ? or h while viewing.
  172.  
  173. This command runs the normal hook `view-mode-hook'."
  174.   (interactive "bView buffer: ")
  175.   (let ((old-buf (current-buffer)))
  176.     (switch-to-buffer buffer-name t)
  177.     (view-mode-enter old-buf nil)))
  178.  
  179. (defun view-buffer-other-window (buffer-name not-return)
  180.   "View BUFFER in View mode in another window.
  181. Return to previous buffer when done, unless NOT-RETURN is non-nil.
  182.  
  183. The usual Emacs commands are not available in View mode; instead,
  184. a special set of commands (mostly letters and punctuation)
  185. are defined for moving around in the buffer.
  186. Space scrolls forward, Delete scrolls backward.
  187. For list of all View commands, type ? or h while viewing.
  188.  
  189. This command runs the normal hook `view-mode-hook'."
  190.   (interactive "bView buffer:\nP")
  191.   (let ((return-to (and not-return (current-window-configuration))))
  192.     (switch-to-buffer-other-window buffer-name)
  193.     (view-mode-enter return-to)))
  194.  
  195. (defun view-mode (&optional arg)
  196.   "Toggle View mode.
  197. With a prefix argument, turn View mode on if the argument is >= zero
  198. and off if it is not.
  199.  
  200. If you use this function to turn on View mode, then subsequently
  201. \"exiting\" View mode does nothing except turn View mode off.  The
  202. other way to turn View mode on is by calling `view-mode-enter';
  203. that is what Lisp programs usually use.
  204.  
  205. Letters do not insert themselves.  Instead these commands are provided.
  206. Most commands take prefix arguments.  Commands dealing with lines
  207. default to \"scroll size\" lines (initially size of window).
  208. Search commands default to a repeat count of one.
  209.  
  210. M-< or <    move to beginning of buffer.
  211. M-> or >    move to end of buffer.
  212. C-v or Space    scroll forward lines.
  213. M-v or DEL    scroll backward lines.
  214. CR or LF    scroll forward one line (backward with prefix argument).
  215. z        like Space except set number of lines for further
  216.            scrolling commands to scroll by.
  217. C-u and Digits    provide prefix arguments.  `-' denotes negative argument.
  218. =        prints the current line number.
  219. g        goes to line given by prefix argument.
  220. / or M-C-s    searches forward for regular expression
  221. \\ or M-C-r    searches backward for regular expression.
  222. n        searches forward for last regular expression.
  223. p        searches backward for last regular expression.
  224. C-@ or .    set the mark.
  225. x        exchanges point and mark.
  226. C-s or s    do forward incremental search.
  227. C-r or r    do reverse incremental search.
  228. @ or '        return to mark and pops mark ring.
  229.           Mark ring is pushed at start of every
  230.           successful search and when jump to line to occurs.
  231.           The mark is set on jump to buffer start or end.
  232. ? or h        provide help message (list of commands).
  233. \\[help-command]        provides help (list of commands or description of a command).
  234. C-n        moves down lines vertically.
  235. C-p        moves upward lines vertically.
  236. C-l        recenters the screen.
  237. q        exit view-mode and return to previous buffer."
  238.   (interactive "P")
  239.   (setq view-mode
  240.     (if (null arg)
  241.         (not view-mode)
  242.       (> (prefix-numeric-value arg) 0)))
  243.   (force-mode-line-update))
  244.  
  245. (defun view-mode-enter (&optional prev-buffer action)
  246.   "Enter View mode, a Minor mode for viewing text but not editing it.
  247. See the function `view-mode' for more details.
  248.  
  249. This function runs the normal hook `view-mode-hook'.
  250.  
  251. \\{view-mode-map}"
  252. ;  Not interactive because dangerous things happen
  253. ;  if you call it without passing a buffer as argument
  254. ;  and they are not easy to fix.
  255. ;  (interactive)
  256.   (setq view-old-buffer-read-only buffer-read-only)
  257.   (setq view-old-Helper-return-blurb
  258.     (and (boundp 'Helper-return-blurb) Helper-return-blurb))
  259.  
  260.   ;; Enable view-exit to make use of the data we just saved
  261.   ;; and to perform the exit action.
  262.   (setq view-mode-auto-exit t)
  263.  
  264.   (setq buffer-read-only t)
  265.   (setq view-mode t)
  266.   (setq Helper-return-blurb
  267.     (format "continue viewing %s"
  268.         (if (buffer-file-name)
  269.             (file-name-nondirectory (buffer-file-name))
  270.             (buffer-name))))
  271.  
  272.   (setq view-exit-action action)
  273.   (setq view-return-here prev-buffer)
  274.   (setq view-exit-position (point-marker))
  275.  
  276.   (beginning-of-line)
  277.   (setq goal-column nil)
  278.  
  279.   (run-hooks 'view-mode-hook)
  280.   (message
  281.      (substitute-command-keys
  282.       "Type \\[help-command] for help, \\[describe-mode] for commands, \\[view-exit] to quit.")))
  283.  
  284. (defun view-exit ()
  285.   "Exit from view-mode.
  286. If you viewed an existing buffer, that buffer returns to its previous mode.
  287. If you viewed a file that was not present in Emacs, its buffer is killed."
  288.   (interactive)
  289.   (setq view-mode nil)
  290.   ;; XEmacs change
  291.   (and view-extent (delete-extent view-extent))
  292.   (force-mode-line-update)
  293.   (cond (view-mode-auto-exit
  294.      (setq buffer-read-only view-old-buffer-read-only)
  295.      (setq view-mode-auto-exit nil)
  296.  
  297.      (goto-char view-exit-position)
  298.      (set-marker view-exit-position nil)
  299.  
  300.      ;; Now do something to the buffer that we were viewing
  301.      ;; (such as kill it).
  302.      (let ((viewed-buffer (current-buffer))
  303.            (action view-exit-action))
  304.        (cond
  305.         ((bufferp view-return-here)
  306.          (switch-to-buffer view-return-here))
  307.         ((window-configuration-p view-return-here)
  308.          (set-window-configuration view-return-here)))
  309.        (if action (funcall action viewed-buffer))))))
  310.  
  311. (defun view-window-size () (1- (window-height)))
  312.  
  313. (defun view-scroll-size ()
  314.   (min (view-window-size) (or view-scroll-size (view-window-size))))
  315.  
  316. (defvar view-mode-hook nil
  317.   "Normal hook run when starting to view a buffer or file.")
  318.  
  319. ;(defun view-last-command (&optional who what)
  320. ;  (setq view-last-command-entry this-command)
  321. ;  (setq view-last-command who)
  322. ;  (setq view-last-command-argument what))
  323.  
  324. ;(defun View-repeat-last-command ()
  325. ;  "Repeat last command issued in View mode."
  326. ;  (interactive)
  327. ;  (if (and view-last-command
  328. ;       (eq view-last-command-entry last-command))
  329. ;      (funcall view-last-command view-last-command-argument))
  330. ;  (setq this-command view-last-command-entry))
  331.  
  332. (defun View-goto-line (line)
  333.   "Move to line LINE in View mode.
  334. Display is centered at LINE.  Sets mark at starting position and pushes
  335. mark ring."
  336.   (interactive "p")
  337.   (push-mark)
  338.   (goto-line line)
  339.   (recenter (/ (view-window-size) 2)))
  340.  
  341. (defun View-scroll-lines-forward (&optional lines)
  342.   "Scroll forward in View mode, or exit if end of text is visible.
  343. No arg means whole window full, or number of lines set by \\[View-scroll-lines-forward-set-scroll-size].
  344. Arg is number of lines to scroll."
  345.   (interactive "P")
  346.   (setq lines
  347.     (if lines (prefix-numeric-value lines)
  348.       (view-scroll-size)))
  349.   (if (and (pos-visible-in-window-p (point-max))
  350.        ;; Allow scrolling backward at the end of the buffer.
  351.        (> lines 0)
  352.        view-mode-auto-exit)
  353.       (view-exit)
  354.     ;; (view-last-command 'View-scroll-lines-forward lines)
  355.     (if (>= lines (view-window-size))
  356.     (scroll-up nil)
  357.       (if (>= (- lines) (view-window-size))
  358.       (scroll-down nil)
  359.     (scroll-up lines)))
  360.     (cond ((pos-visible-in-window-p (point-max))
  361.        (goto-char (point-max))
  362.        (message "%s"
  363.             (substitute-command-keys
  364.              "End.  Type \\[view-exit] to quit viewing."))))
  365.     (move-to-window-line -1)
  366.     (beginning-of-line)))
  367.  
  368. (defun View-scroll-lines-forward-set-scroll-size (&optional lines)
  369.   "Scroll forward LINES lines in View mode, setting the \"scroll size\".
  370. This is the number of lines which \\[View-scroll-lines-forward] and \\[View-scroll-lines-backward] scroll by default.
  371. The absolute value of LINES is used, so this command can be used to scroll
  372. backwards (but \"scroll size\" is always positive).  If LINES is greater than
  373. window height or omitted, then window height is assumed.  If LINES is less
  374. than window height then scrolling context is provided from previous screen."
  375.   (interactive "P")
  376.   (if (not lines)
  377.       (setq view-scroll-size (view-window-size))
  378.     (setq lines (prefix-numeric-value lines))
  379.     (setq view-scroll-size
  380.       (min (if (> lines 0) lines (- lines)) (view-window-size))))
  381.   (View-scroll-lines-forward lines))
  382.  
  383. (defun View-scroll-one-more-line (&optional arg)
  384.   "Scroll one more line up in View mode.
  385. With ARG scroll one line down."
  386.   (interactive "P")
  387.   (View-scroll-lines-forward (if (not arg) 1 -1)))
  388.  
  389. (defun View-scroll-lines-backward (&optional lines)
  390.   "Scroll backward in View mode.
  391. No arg means whole window full, or number of lines set by \\[View-scroll-lines-forward-set-scroll-size].
  392. Arg is number of lines to scroll."
  393.   (interactive "P")
  394.   (View-scroll-lines-forward (if lines
  395.                  (- (prefix-numeric-value lines))
  396.                    (- (view-scroll-size)))))
  397.   
  398. (defun View-search-regexp-forward (n regexp)
  399.   "Search forward for Nth occurrence of REGEXP.
  400. Displays line found at center of window.  REGEXP is remembered for
  401. searching with \\[View-search-last-regexp-forward] and \\[View-search-last-regexp-backward].  Sets mark at starting position and pushes mark ring.
  402.  
  403. The variable `view-highlight-face' controls the face that is used
  404. for highlighting the match that is found."
  405.   (interactive "p\nsSearch forward (regexp): ")
  406. ;;;(view-last-command 'View-search-last-regexp-forward n)
  407.   (view-search n (if (equal regexp "") view-last-regexp regexp)))
  408.  
  409. (defun View-search-regexp-backward (n regexp)
  410.   "Search backward from window start for Nth instance of REGEXP.
  411. Displays line found at center of window.  REGEXP is remembered for
  412. searching with \\[View-search-last-regexp-forward] and \\[View-search-last-regexp-backward].  Sets mark at starting position and pushes mark ring.
  413.  
  414. The variable `view-highlight-face' controls the face that is used
  415. for highlighting the match that is found."
  416.   (interactive "p\nsSearch backward (regexp): ")
  417.   (View-search-regexp-forward (- n)
  418.                   (if (equal regexp "") view-last-regexp regexp)))
  419.  
  420. (defun View-search-last-regexp-forward (n)
  421.   "Search forward from window end for Nth instance of last regexp.
  422. Displays line found at center of window.  Sets mark at starting position
  423. and pushes mark ring.
  424.  
  425. The variable `view-highlight-face' controls the face that is used
  426. for highlighting the match that is found."
  427.   (interactive "p")
  428.   (if view-last-regexp
  429.       (View-search-regexp-forward n view-last-regexp)
  430.     (error "No previous View-mode search")))
  431.  
  432. (defun View-search-last-regexp-backward (n)
  433.   "Search backward from window start for Nth instance of last regexp.
  434. Displays line found at center of window.  Sets mark at starting position and
  435. pushes mark ring.
  436.  
  437. The variable `view-highlight-face' controls the face that is used
  438. for highlighting the match that is found."
  439.   (interactive "p")
  440.   (if view-last-regexp
  441.       (View-search-regexp-backward n view-last-regexp)
  442.     (error "No previous View-mode search")))
  443.  
  444. (defun View-back-to-mark (&optional ignore)
  445.   "Return to last mark set in View mode, else beginning of file.
  446. Displays line at center of window.  Pops mark ring so successive
  447. invocations return to earlier marks."
  448.   (interactive)
  449.   (goto-char (or (mark t) (point-min)))
  450.   (pop-mark)
  451.   (recenter (/ (view-window-size) 2)))
  452.          
  453. (defun view-search (times regexp)
  454.   (setq view-last-regexp regexp)
  455.   (let (where)
  456.     (save-excursion
  457.       (move-to-window-line (if (< times 0) 0 -1))
  458.       (if (re-search-forward regexp nil t times)
  459.       (setq where (point))))
  460.     (if where
  461.     (progn
  462.       (push-mark)
  463.       (goto-char where)
  464.       ;; XEmacs change.
  465.       (if view-extent
  466.           (set-extent-endpoints view-extent (match-beginning 0)
  467.                     (match-end 0))
  468.         (setq view-extent
  469.           (make-extent (match-beginning 0) (match-end 0))))
  470.       (set-extent-face view-extent view-highlight-face)
  471.       (beginning-of-line)
  472.       (recenter (/ (view-window-size) 2)))
  473.       (message "Can't find occurrence %d of %s" times regexp)
  474.       (sit-for 4))))
  475.  
  476.  
  477. (provide 'view)
  478.  
  479. ;;; view.el ends here
  480.